using System;
using System.Collections.Generic;
using System.Text;
using Evolution.GUI.Components;
using Evolution.Engine.GUI.Components;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Managers;

namespace Evolution.Engine.Test.GUI
{
    public class TestForm : Form
    {
        Lable _Message;
        Lable _RotationLable;
        EditBox _Rotation;
        EditBox _Object;
        Lable _ObjectLable;
        int number = 0;

        BaseObject _obj = null;

        public TestForm()
            : base("TestForm")
        {
            Size = new SizeF(300, 400);
            Location = new Vector2(100, 100);
            SetCaption("Select Character");

            LoadComponents();
        }

        private void LoadComponents()
        {
            _ObjectLable = new Lable(this, "Object Name", new Vector2(10, 10), "Default");
            _RotationLable = new Lable(this, "Rotation", new Vector2(10, 35), "Default");

            _Object = new EditBox(this, "Object", "", new Vector2(150, 10), new Vector2(100, 20), "Default", Color.Black, 20);
            _Rotation = new EditBox(this, "Rotation", "", new Vector2(150, 35), new Vector2(100, 20), "Default", Color.Black, 20);

            _Controls.Add(_ObjectLable);
            _Controls.Add(_RotationLable);
            _Controls.Add(_Object);
            _Controls.Add(_Rotation);

            Pushbutton pb = new Pushbutton("PButton", "Find", this, new Vector2(10, 60), new Vector2(100, 30));
            pb.OnClick += new ButtonClick(pb_OnClick);
            _Controls.Add(pb);

            Pushbutton pb1 = new Pushbutton("PButton1", "Write Image", this, new Vector2(10, 135), new Vector2(200, 30));
            pb1.OnClick += new ButtonClick(pb1_OnClick);
            _Controls.Add(pb1);

            _Message = new Lable(this, "No object selected", new Vector2(10, 100), "Default");
            _Controls.Add(_Message);
        }

        void pb1_OnClick(object sender)
        {
            Texture2D renderTargetTexture;

            renderTargetTexture = new Texture2D(
                GameEngine.Instance.Device,
                GameEngine.Instance.Device.PresentationParameters.BackBufferWidth,
                GameEngine.Instance.Device.PresentationParameters.BackBufferHeight,
                0,
                ResourceUsage.ResolveTarget | ResourceUsage.AutoGenerateMipMap,
                GameEngine.Instance.Device.PresentationParameters.BackBufferFormat,
                ResourceManagementMode.Manual
                );

            GameEngine.Instance.Device.ResolveBackBuffer(renderTargetTexture);

            renderTargetTexture.Save("ScreenShot" + number.ToString() + ".jpg", ImageFileFormat.Jpg);
            number++;

            renderTargetTexture.Dispose();
            renderTargetTexture = null;
        }
        void pb_OnClick(object sender)
        {
            if (string.IsNullOrEmpty(_Object.Text))
            {
                _Message.Text = "Fill a name";
                return;
            }

            _obj = SceneManager.Instance.CurrentScene.Find(_Object.Text);

            if( _obj != null )
                _Message.Text = "Object found";
            else
                _Message.Text = "Could not find it";
        }
    }
}
